var setting = {
	view : {
		selectedMulti : false
	},
	edit : {
		enable : true,
		showRemoveBtn : false,
		showRenameBtn : false,
		drag : {
			isCopy : false,
			isMove : true,
			prev : true,
			next : true,
			inner : true
		}
	},
	data : {
		keep : {
			parent : false,
			leaf : false
		},
		simpleData : {
			enable : true,
			pIdKey : "parentid",
			rootPid : null
		}
	},
	callback : {
		beforeDrag : beforeDrag,
		beforeRemove : beforeRemove,
		beforeRename : beforeRename,
		onClick : onClick
	}
},
zNodes, log, className = "dark", treeNodeTmp, delNodes = [], sNodes = [], newCount = 1;
function beforeDrag(treeId, treeNodes) {
	return true;
}
function beforeRemove(treeId, treeNode) {
	var a;
	for(var i=0;i<zNodes.length;i++){
		a=zNodes[i];
		if(a.id==treeNode.id){
			delNodes.push(a);
			if(treeNode.isParent){
				for(var j=0;j<zNodes.length;j++){
					a=zNodes[j];
					if(a.parentid==treeNode.id){
						delNodes.push(a);
					}
				}
			}
		}
	}
	return true;
}
function onClick(e, treeId, treeNode) {
	treeNodeTmp=treeNode;
	$("#menuId").val(treeNode.id);
	$("#name").val(treeNode.name);
	$("#menuurl").val(treeNode.menuurl);
	$("#menuclass").val(treeNode.menuclass);
	if(treeNode.status==1){
		$("#senable").click();
	}else{
		$("#sdisable").click();
	}
}
function beforeRename(treeId, treeNode, newName) {
	if (newName.length == 0) {
		showAlert("节点名称不能为空.");
		var zTree = $.fn.zTree.getZTreeObj("treeDemo");
		setTimeout(function() {
			zTree.editName(treeNode)
		}, 10);
		return false;
	}
	return true;
}
function addParent() {
	var zTree = $.fn.zTree.getZTreeObj("treeDemo"), treeNode;
	getMaxId();
	treeNode = zTree.addNodes(null, {
		id : (++newCount),
		pId : 0,
		isParent : true,
		name : "new node" + (newCount),
		menuurl : "#",
		menuclass : null,
		parentid : null,
		orders : 1,
		status : 1
	});
	if (treeNode) {
		treeNode=treeNode[0];
		zTree.selectNode(treeNode);
		onClick(null, null, treeNode);
	} else {
		showAlert("叶子节点被锁定，无法增加子节点");
	}
}
function addChild(){
	var zTree = $.fn.zTree.getZTreeObj("treeDemo"),
	nodes = zTree.getSelectedNodes(),
	treeNode = nodes[0];
	if(treeNode&&treeNode.level==0){
		getMaxId();
		if(!treeNode.isParent){
			treeNode.isParent=true;
			zTree.updateNode(treeNode);
		}
		treeNode = zTree.addNodes(treeNode, {
			id : (++newCount),
			pId : treeNode.id,
			isParent : false,
			name : "new node" + (newCount),
			menuurl : "#",
			menuclass : null,
			parentid : treeNode.id,
			orders : 1,
			status : 1
		});
		if (treeNode) {
			treeNode=treeNode[0];
			zTree.selectNode(treeNode);
			onClick(null, null, treeNode);
		} else {
			showAlert("叶子节点被锁定，无法增加子节点");
		}
	}else{
		showAlert("请选择一个父节点!");
	}
}
function getMaxId(){
	$.each(zNodes, function(i,n){
		if(n.id>newCount){
			newCount=n.id;
		}
	});
}
function remove(e) {
	var zTree = $.fn.zTree.getZTreeObj("treeDemo"), nodes = zTree
			.getSelectedNodes(), treeNode = nodes[0];
	if (nodes.length == 0) {
		showAlert("请先选择一个节点");
		return;
	}
	var callbackFlag = $("#callbackTrigger").attr("checked");
	zTree.removeNode(treeNode, true);
}
function success(){
	if(treeNodeTmp){
		treeNodeTmp.id=$("#menuId").val();
		treeNodeTmp.name=$("#name").val();
		treeNodeTmp.menuurl=$("#menuurl").val();
		treeNodeTmp.menuclass=$("#menuclass").val();
		treeNodeTmp.status=$("input:radio:checked").val();
		var zTree = $.fn.zTree.getZTreeObj("treeDemo");
		zTree.editName(treeNodeTmp);
		zTree.cancelEditName(treeNodeTmp.name);
	}
}
//将菜单树处理成json
function saveMenu(){
	sNodes=[];
	var treeObj = $.fn.zTree.getZTreeObj("treeDemo"),
	nodes = treeObj.getNodes(),
	aNodes = [], uNodes = [], dNodes = [];
	nodes = treeObj.transformToArray(nodes);
	/**
	 * 对结果进行解析,对增加或删除的菜单项增加标识
	 */
	$.each(nodes, function(i, n){
		var nn = {};
		nn.id=n.id;
		nn.name=n.name;
		nn.menuurl=n.menuurl;
		nn.menuclass=n.menuclass;
		nn.status=n.status;
		nn.parentid=n.parentid;
		nn.orders=n.getIndex();
		if(checkNodeIsInzNodes(nn.id)){
			uNodes.push(nn);
		}else{
			aNodes.push(nn);
		}
	});
	//增加以删除菜单项
	if(delNodes&&delNodes.length>0){
		var b;
		for(var j=0;j<delNodes.length;j++){
			b=delNodes[j];
			dNodes.push(b);
		}
	}
	sNodes.push(aNodes);sNodes.push(uNodes);sNodes.push(dNodes);
	return JSON.stringify(sNodes);
}
//提交编辑结果
function submitNodes(data){
	$.ajax({
		url: "system/changeMenus",
		type: "POST",
		async: false,
		timeout: 3000,
		data: {"menus":saveMenu()},
		dataType: "json",
		success: function(data){
			if(data){
				if(data.code==100){
					showSuccess("保存成功!");
				}else{
					showAlert(data.message);
				}
			}else{
				showAlert("菜单保存失败!");
			}
		},
		error: function(){
			showAlert("菜单保存失败!");
		}
	});
}
function checkNodeIsInzNodes(id){
	for(var i=0;i<zNodes.length;i++){
		if(zNodes[i].id==id){
			return true;
		}
	}
	return false;
}
//获取菜单树
function getNodes(){
	$.ajax({
		url: "system/getAllMenus",
		async: false,
		timeout: 3000,
		dataType: "json",
		success: function(data){
			if(data){
				if(data.code == 100){
					zNodes = data.message;
				}else{
					showAlert(data.message);
				}
			}else{
				showAlert("获取列表失败!");
			}
		},
		error: function(){
			showAlert("获取菜单失败!");
		}
	});
}
$(document).ready(function() {
	getNodes();
	$.fn.zTree.init($("#treeDemo"), setting, zNodes);
	$("#remove").bind("click", remove);
	$("#btn_success").bind("click", success);
	$("#addParentMenu").bind("click", addParent);
	$("#addChildMenu").bind("click", addChild);
	$("#saveMenu").bind("click", submitNodes);
});